//
//  TT_TabBarView.m
//  ExhibitionHall
//
//  Created by 王洋 on 2019/5/28.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "TT_TabBarItem.h"
#import "TT_TabBarView.h"

@interface TT_TabBarView ()

@property (nonatomic, strong) TT_TabBarItem *HomeItem;
@property (nonatomic, strong) TT_TabBarItem *CategoryItem;
@property (nonatomic, strong) TT_TabBarItem *ARItem;
@property (nonatomic, strong) TT_TabBarItem *MineItem;

@property (nonatomic, strong) TT_TabBarItem *selectedItem;

@property (nonatomic, strong) UIButton *categoryShowBtn;

@end

@implementation TT_TabBarView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {

        [self addSubview:self.HomeItem];
        [self addSubview:self.CategoryItem];
        [self addSubview:self.ARItem];
        [self addSubview:self.MineItem];
        [self addSubview:self.categoryShowBtn];
        self.categoryShowBtn.hidden = YES;
        self.HomeItem.enabled = NO;
        self.selectedItem = self.HomeItem;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.HomeItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(tabbarItemInstance);
        make.width.height.mas_equalTo(tabbarItemWidth);
    }];
    
    [self.CategoryItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.HomeItem.mas_bottom).offset(tabbarItemInstance);
        make.width.height.mas_equalTo(tabbarItemWidth);
    }];
    
    [self.categoryShowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.HomeItem.mas_bottom).offset(tabbarItemInstance);
        make.width.height.mas_equalTo(tabbarItemWidth);
    }];
    [self.ARItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.CategoryItem.mas_bottom).offset(tabbarItemInstance);
        make.width.height.mas_equalTo(tabbarItemWidth);
    }];
    
    [self.MineItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.bottom.mas_equalTo(self).offset(-20);
        make.width.height.mas_equalTo(tabbarItemWidth);
    }];
}

- (TT_TabBarItem *)HomeItem {
    if (!_HomeItem) {
        _HomeItem = [[TT_TabBarItem alloc] init];
        _HomeItem.tag = 0;
        [_HomeItem setTitle:@"首页"];
        [_HomeItem imageSetting:@"home_nor" selectedImage:@"home_sel"];
        [_HomeItem addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
    }
    return _HomeItem;
}

- (TT_TabBarItem *)CategoryItem {
    if (!_CategoryItem) {
        _CategoryItem = [[TT_TabBarItem alloc] init];
        _CategoryItem.tag = 1;
        [_CategoryItem setTitle:@"分类"];
        [_CategoryItem imageSetting:@"category_nor" selectedImage:@"category_sel"];
        [_CategoryItem addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
    }
    return _CategoryItem;
}

- (UIButton *)categoryShowBtn {
    if (!_categoryShowBtn) {
        _categoryShowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_categoryShowBtn setBackgroundColor:[UIColor clearColor]];
        [_categoryShowBtn addTarget:self action:@selector(showCategory) forControlEvents:UIControlEventTouchDown];
    }
    return _categoryShowBtn;
}

- (TT_TabBarItem *)ARItem {
    if (!_ARItem) {
        _ARItem = [[TT_TabBarItem alloc] init];
        _ARItem.tag = 2;
        [_ARItem setTitle:@"AR购"];
        [_ARItem imageSetting:@"AR_nor" selectedImage:@"AR_sel"];
        [_ARItem addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
    }
    return _ARItem;
}

- (TT_TabBarItem *)MineItem {
    if (!_MineItem) {
        _MineItem = [[TT_TabBarItem alloc] init];
        _MineItem.tag = 3;
        [_MineItem setTitle:@"我的"];
        [_MineItem imageSetting:@"mine_nor" selectedImage:@"mine_sel"];
        [_MineItem addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
    }
    return _MineItem;
}

- (void)showCategory {
    [[NSNotificationCenter defaultCenter]postNotificationName:categoryShowCategoryNoti object:nil userInfo:nil];
}

- (void)buttonClick:(TT_TabBarItem *)btn {
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[NSString stringWithFormat:@"%ld",(long)btn.tag] forKey:@"action"];
    
    if (btn.tag != 2) {
        
            self.selectedItem.enabled = YES;
//            让当前选中按钮变成不可用状态
            btn.enabled = NO;
            //让当前按钮赋值给属性
            self.selectedItem = btn;
        
    } else {
        //AR按钮不被选中
    }
    
    //当分类按钮被选中的时候     分类按钮取消点击      控制分类两个tableview显示隐藏的按钮显示出来
    if (self.selectedItem.tag == 1) {
        self.categoryShowBtn.hidden = NO;
    } else {
        self.categoryShowBtn.hidden = YES;
    }
    
    [[NSNotificationCenter defaultCenter]postNotificationName:tabbarItemSelectNoti object:nil userInfo:[dic mutableCopy]];
}

@end
